package cn.wen.eeayjoke.refresh;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshKernel;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.util.DensityUtil;

import cn.wen.eeayjoke.R;

/**
 * 自定义的刷新头
 */

public class AppHeader extends RelativeLayout implements RefreshHeader {
    private ImageView mIvRefresh;
    private AnimationDrawable mAnim;
    private RefreshKernel kernel;
    DensityUtil mDensityUtil = new DensityUtil();

    public AppHeader(Context context) {
        this(context, null);
    }

    public AppHeader(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AppHeader(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View header = LayoutInflater.from(context).inflate(R.layout.layout_app_header, this, false);
        mIvRefresh = header.findViewById(R.id.iv_refresh);
        mAnim = (AnimationDrawable) mIvRefresh.getDrawable();
        addView(header);
    }


    @NonNull
    @Override
    public View getView() {
        return this;
    }

    @NonNull
    @Override
    public SpinnerStyle getSpinnerStyle() {
        return SpinnerStyle.Translate;
    }

    @Override
    public void setPrimaryColors(int... colors) {

    }

    @Override
    public void onInitialized(@NonNull RefreshKernel kernel, int height, int extendHeight) {
        this.kernel = kernel;
        kernel.requestDefaultHeaderTranslationContent(true);
    }

    @Override
    public void onPulling(float percent, int offset, int height, int extendHeight) {
        if (percent >= 0 && percent <= 1) {
            mIvRefresh.setScaleX(percent);
            mIvRefresh.setScaleY(percent);
        }
    }

    @Override
    public void onReleasing(float percent, int offset, int height, int extendHeight) {

    }

    @Override
    public void onReleased(RefreshLayout refreshLayout, int height, int extendHeight) {
        mIvRefresh.setScaleX(1);
        mIvRefresh.setScaleY(1);
        kernel.animSpinner(mDensityUtil.dip2px(50));
        mAnim.start();
    }

    @Override
    public void onStartAnimator(@NonNull RefreshLayout refreshLayout, int height, int extendHeight) {

    }

    @Override
    public int onFinish(@NonNull RefreshLayout refreshLayout, boolean success) {
        mAnim.stop();
        mIvRefresh.clearAnimation();
        kernel.animSpinner(0);
        return 0;
    }

    @Override
    public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {

    }

    @Override
    public boolean isSupportHorizontalDrag() {
        return false;
    }

    @Override
    public void onStateChanged(RefreshLayout refreshLayout, RefreshState oldState, RefreshState newState) {
        switch (newState) {
            case Refreshing:
                kernel.animSpinner(mDensityUtil.dip2px(50));
                break;
        }
    }
}
